<cfcomponent extends="mxunit.framework.TestCase"
            hint="">

  <cffunction name="setUp"  output="true">
    <cfset _i = structNew() />
    <cfset _i.component = createObject("component","org.coldquery.virtualquery.VirtualQuery") />
  </cffunction>

  <cffunction name="testInit"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var queryloader= createObject("component","DummyQueryLoader").init() />
    <cfset initStruct.items= arrayNew(1) />

    <cftry>
      <cfset _i.component.init(queryloader=queryLoader) />
      <cfcatch>
        <cfset fail(message="Init of component with dummy query loader failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.getItems() />
    <cfset assertTrue(message="Get Items must return an array",condition=isArray(result)) />
    <cfset assertNotEquals(_i.component.getLength(),4,"Dummy Query should report size of 3 not 4") />
    <cfset assertEquals(_i.component.getLength(),3,"Dummy Query should report size of 3") />

  </cffunction>

  <cffunction name="testGetItemsArray"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset var queryloader= createObject("component","DummyQueryLoader").init() />
    <cftry>
      <cfset _i.component.init(queryloader=queryloader) />
      <cfcatch>
        <cfset fail(message="Init of component with DummyQueryLoader failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.get(2) /> <!--- should load items and get row with first name chris --->
    <cfset assertEquals(result.name,"chris","Should retrieve the value of Chris.  The second row in the query") />
  </cffunction>

  <cffunction name="testVirtualQuerySort"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset var queryloader= createObject("component","DummyQueryLoader").init() />

    <cftry>
      <cfset _i.component.init(queryloader=queryloader) />
      <cfcatch>
        <cfset fail(message="Init of component with populated Query failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.sortByColumn("name","textnocase","asc") />
    <cfset assertEquals(getMetaData(result).name,"org.coldquery.ColdQuery","Should return type of org.coldquery.ColdQuery") />
    <cfset assertEquals(result.get(1).name,"Chris","Sorted by name row 1 column 1 should be Chris") />
    <cfset assertEquals(result.get(2).name,"Joe",  "sorted by name row 2 column 1 should be Joe") />
    <cfset assertEquals(result.get(3).name,"lee",  "sorted by name row 3 column 1 should be Lee") />
  </cffunction>

  <cffunction name="testConcat"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset initStruct.items= structNew() />
    <cfset initStruct.items.key1 = "myValue" />
    <cfset initStruct.items.key2 = structNew() />
    <cfset initStruct.items.key3 = ArrayNew(1) />
    <cfset result = _i.component.new(argumentcollection=initStruct) />
    <cfset result = result.concat(result.getItems()).sortByColumn("name","string","desc") />

    <cfset assertEquals(message="Length should be 6",expected='6',actual=result.getLength()) />
    <cfset assertEquals(message="name key at position 1 should by 'key3'",expected='KEY3',actual=result.get(1).name) />
    <cfset assertEquals(message="name key at position 2 should by 'key3'",expected='KEY3',actual=result.get(2).name) />
    <cfset assertEquals(message="name key at position 5 should by 'key1'",expected='KEY1',actual=result.get(5).name) />
    <cfset assertEquals(message="name key at position 6 should by 'key1'",expected='KEY1',actual=result.get(6).name) />

  </cffunction>
</cfcomponent>

